﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using App;
using AMSTrademarkClient.Cases.Details;
using DevComponents.DotNetBar;
using AMS.UI.Forms;

namespace AMSTrademarkClient.Cases
{
    public partial class FrmEditRegisterCase : BaseForm
    {
        public FrmEditRegisterCase()
        {
            InitializeComponent();
            halignHelper = new HorizonCentralAlignHelper(pnlCmd, new Control[] { });
            tabMain.Tabs.Clear();
        }

        HorizonCentralAlignHelper halignHelper;
        static Dictionary<int, Type> workFormMapping;

        void ShowForm(int index)
        {
            Form frm = null;
            switch (index)
            {
                case 0:
                    frm = new FrmBasicInfo();
                    break;
                    // TODO: add impl here
                default:
                    break;
            }

            if (frm != null)
            {
                ShowForm(frm);
            }
        }

        TabItem ShowForm(Form frm)
        {
            TabControlPanel panel = new TabControlPanel();
            TabItem item = new TabItem(this.components);
            item.Text = frm.Text;
            item.Icon = frm.Icon;
            item.AttachedControl = panel;
            item.Tag = frm.Tag;
            panel.TabItem = item;
            panel.Dock = DockStyle.Fill;
            tabMain.Tabs.Add(item);
            tabMain.Controls.Add(panel);
            return ShowForm(frm, panel);
        }

        TabItem ShowForm(Form frm, TabControlPanel panel)
        {
            frm.MdiParent = this;
            frm.Parent = panel;
            panel.Tag = frm.Tag;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.AutoScroll = true;
            frm.Show();
            return panel.TabItem;
        }

        bool CloseForm(int tag)
        {
            bool bFound = false;
            foreach (Form frm in this.MdiChildren)
            {
                if (frm.Tag != null && frm.Tag.Equals(tag))
                {
                    frm.Close();
                    bFound = true;
                    break;
                }
            }

            return bFound;
        }

        private void lstNav_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowPageByTag(lstNav.SelectedIndex);
        }

        void ShowPageByTag(int tag)
        {
            bool bFound = false;
            foreach (TabItem item in tabMain.Tabs)
            {
                if (item.Tag != null && item.Tag.Equals((object)tag))
                {
                    tabMain.SelectedTab = item;
                    bFound = true;
                    break;
                }
            }

            if (!bFound)
            {
                Form frm = CreateWorkForm(tag);
                if (frm != null)
                {
                    frm.Tag = tag;
                    TabItem item = ShowForm(frm);
                    tabMain.SelectedTab = item;
                }
            }
        }

        private Form CreateWorkForm(int tag)
        {
            if (workFormMapping == null)
            {
                workFormMapping = new Dictionary<int, Type>();
                workFormMapping.Add(0, typeof(FrmBasicInfo));
                workFormMapping.Add(1, typeof(FrmTrademarkInfo));
                workFormMapping.Add(2, typeof(FrmCustomerInfo));
                workFormMapping.Add(3, typeof(FrmApplicants));
                workFormMapping.Add(4, typeof(FrmContactors));
                workFormMapping.Add(5, typeof(FrmPriorityInfo));
                workFormMapping.Add(7, typeof(FrmCaseWorkers));
            }

            if (workFormMapping.ContainsKey(tag))
            {
                Type type = workFormMapping[tag];
                return Activator.CreateInstance(type) as Form;
            }
            else
            {
                return null;
            }
        }

        private void tabMain_TabItemClose(object sender, TabStripActionEventArgs e)
        {
            if (sender == tabMain)
            {
                TabItem item = tabMain.SelectedTab;
                tabMain.Tabs.Remove(item);
                CloseForm((int)item.Tag);
            }
        }

        private void FrmEditRegisterCase_Load(object sender, EventArgs e)
        {

        }

    }
}
